<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改资料</title>
    <link rel="stylesheet" type="text/css" href="/static/hplus/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="/static/hplus/css/plugins/iCheck/custom.css">
    <link rel="stylesheet" type="text/css" href="/static/hplus/css/style.css">
    <link rel="stylesheet" type="text/css" href="/static/layer/skin/layer.css">
    <style type="text/css">
        .form-control {width: 200px;}
        .col-sm-10 {width: 250px;display: inline-block;}
        .col-sm-2 {text-align: right;width: 125px;}
    </style>
</head>
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>修改资料</h5>
                </div>
                <div class="ibox-content">
                    <form class="form-horizontal" onsubmit="return false">
                        <input type="hidden" name="id" value="{$user.id}">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">登录账号</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="username" name="username" value="{$user.username}" {if($admin.role_id!=='1')}readonly="1"{/if}>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">登录密码</label>
                            <div class="col-sm-10">
                                <input type="password" class="form-control" id="pwd1" name="pwd1">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">确认密码</label>
                            <div class="col-sm-10">
                                <input type="password" class="form-control" id="pwd2" name="pwd2">
                            </div>
                        </div>
                        {if($user.role_id!=='1')}
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">用户角色</label>
                            <div class="col-sm-10">
                                {if($admin.role_id==='1')}
                                <select id="role_id" name="role_id" class="form-control">
                                    <option value="0">…请选择用户角色…</option>
                                    {foreach $roles as $r}
                                    <option value="{$r.id}" {if($user.role_id===$r.id)}selected="selected"{/if}>{$r.role_name}</option>
                                    {/foreach}
                                </select>
                                {else/}
                                <input type="text" class="form-control" value="{$role_name}" disabled="disabled">
                                {/if}
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">状态</label>
                            <div class="col-sm-10">
                                {if($admin.role_id==='1')}
                                <select id="status" name="status" class="form-control">
                                    <option value="1" {if($user.status===1)}selected="selected"{/if}>正常</option>
                                    <option value="4" {if($user.status===4)}selected="selected"{/if}>禁用</option>
                                </select>
                                {else/}
                                <input type="text" class="form-control" value="{$user.status_txt}" disabled="disabled">
                                {/if}
                            </div>
                        </div>
                        {/if}
                        {if($t&&$user.username===$admin.username)}
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-2">
                                <input class="btn btn-primary" type="button" data-action="save" value="保存内容">
                                {if($admin.role_id==='1')}
                                <input class="btn btn-white" type="button" data-action="cancel" value="取消">
                                {/if}
                            </div>
                        </div>
                        {/if}
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/javascript" src="/static/hplus/js/jquery.min.js?v={php}echo time(){/php}"></script>
<script type="text/javascript" src="/static/layer/layer.js?v={php}echo time(){/php}"></script>
<script type="text/javascript">
    $(function () {
        $('input[data-action]').click(function () {
            let $action = $(this).data('action');
            if ($action === 'save') {
                let pwd1 = $('#pwd1'), pwd2 = $('#pwd2'), role_id = $('#role_id').val(), status = $('#status').val();
                if (pwd1.val() !== '' && pwd1.val() !== pwd2.val()) {
                    layer.msg('再次输入的密码不一致', {icon: 5, time: 1000}, function () {
                        pwd1.focus()
                    });
                    return false
                }
                if (role_id <= 0) {
                    layer.msg('请选择用户角色', {icon: 5, time: 1000});
                    return false
                }
                $.ajax({
                    type: 'post', cache: false, dataType: 'json',
                    url: '/admin/edit',
                    data: {
                        'id': '{$user.id}',
                        'username': $('#username').val(),
                        'pwd1': pwd1.val(),
                        'pwd2': pwd2.val(),
                        'role_id': role_id,
                        'status': status
                    },
                    success: function (data) {
                        if (data.message === '密码重置成功，请重新登录') {
                            layer.msg(data.message, {icon: 6, time: 1000}, function () {
                                window.top.location.href = '/login/index?action=logout'
                            })
                        } else if (data.code === 200) {
                            layer.msg(data.message, {icon: 6, time: 1000});
                        } else {
                            layer.msg(data.message, {icon: 5, time: 1000});
                        }
                    }
                });
            } else if ($action === 'cancel') {
                location.href = '/admin/lists';
            }
        });
    });
</script>
</html>
